import { asyncRoutes, constantRoutes } from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {

  if (route.meta && route.meta.roles) {
    return roles.some(role => route.meta.roles.includes(role))
  } else {
    return true
  }
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach(route => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

// const actions = {
//   generateRoutes({ commit }, roles) {
//     return new Promise(resolve => {
//       let accessedRoutes
//       if (roles.includes('admin')) {
//
//         accessedRoutes = asyncRoutes || []
//       } else {
//
//         accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
//       }
//       console.log(accessedRoutes)
//       // 便利数组
//       // 以上代码是自带的
//       // 以下是有用代码
//       function List(accessedRoutes,Urlkey){
//         console.log(Urlkey,'便利数组中的Urlkey')
//         var data = new Array
//         var dataCchildren = new Array
//         for(var i=0;i<accessedRoutes.length;i++){
//           // for(var i=0;i<accessedRoutes.length-1;i++){
//           console.log('1')
//           for(var l=0;l<Urlkey.length;l++){
//             if(accessedRoutes[i].meta.Kid == Urlkey[l]){
//               data.push(accessedRoutes[i])
//             }else{
//             }
//           }
//         }
//         for(var k=0;k<data.length;k++){
//           console.log('2')
//           for(var a=0;a<data[k].children.length;a++){
//             for(var b=0;b<Urlkey.length;b++){
//               if(data[k].children[a].meta.Kid == Urlkey[b]){
//                 console.log(data[k].children[a])
//                 dataCchildren.push(data[k].children[a])
//               }
//             }
//           }
//         }
//         console.log(data)
//         console.log(dataCchildren)
//         for(var c=0;c<data.length;c++){
//           if(data[c].children.length>1){
//             data[c].children = []
//             for(var e=0;e<dataCchildren.length;e++){
//               if(dataCchildren[e].meta.Fid == data[c].meta.Kid){
//                 console.log(dataCchildren[c])
//                 data[c].children.push(dataCchildren[e])
//               }
//             }
//           }
//         }
//         return data
//       }
//       if (localStorage.getItem('usertype') == 1) {
//
//         // var Urlkey = [1,2,11,13,15,17,64,101,102,103,104]
//         var Urlkey = [77,1]
//         var data=List(accessedRoutes,Urlkey)
//         commit('SET_ROUTES', data)
//         resolve(data)
//         return
//       }
//       else{
//         console.log('2')
//         var Urlkey = []
//         var data=List(accessedRoutes,Urlkey)
//         console.log(data)
//         commit('SET_ROUTES', data)
//         resolve(data)
//         return
//
//       }
//       // 结束
//       // commit('SET_ROUTES', accessedRoutes)
//       // resolve(accessedRoutes)
//     })
//   }
// }

export default {
  namespaced: true,
  state,
  mutations,
  //actions
}
